package com.csx.governance.traffic.core.metadata;

import cn.hutool.core.util.StrUtil;
import org.springframework.core.env.Environment;

/**
 * 从环境变量取服务元数据的值
 * @author cuisongxu
 * @date 2023/3/5 16:18
 */
public class EnvironmentServiceMetadataProvider implements ServiceMetadataProvider {

    private final Environment environment;

    public EnvironmentServiceMetadataProvider(Environment environment) {
        this.environment = environment;
    }

    @Override
    public String get(String key) {

        // 1. 先取出自定义配置中的元数据
        String property = environment.getProperty("traffic.governance.service.metadata." + key);
        if(!StrUtil.isBlank(property)) {
            return property;
        }

        // 2. 再取出Nacos官方设定的环境变量的值
        String nacosProperty = environment.getProperty("spring.cloud.nacos.discovery.metadata." + key);
        if(!StrUtil.isBlank(nacosProperty)) {
            return nacosProperty;
        }

        return null;
    }
}
